﻿--[[
    @Brief    新版验证码逻辑
    @Author    hqzheng
    @Date    2017-11-07
--]]

TNewPictCheck = 
{
    InitView = function(nRootID)
        TNewPictCheck.nRootID = nRootID;
    end;

    UnInitView = function()
         TGPLog.LogE("NewPictChk", "UnInitView.");
        TNewPictCheck.HideView();
        TNewPictCheck.pChkView = nil;
    end;

    OpenPage = function (control_id)
        local pWebCtrl = ui_utility.get_ctrl_ptr_by_path(control_id, "QBWebBrowser");
        TGPLog.LogE("NewPictChk", "OpenPage:" ..TNewPictCheck.url);
        local ui_str = U8toCUIStr(TNewPictCheck.url);
        local pWebCtrl = tolua.cast(pWebCtrl, "Tenio::IQBlinkWebBrowser");
        if pWebCtrl then
            local ui_loaded_str = U8toCUIStr("NotifyPageLoaded");
            pWebCtrl:CreateJsCallback(ui_loaded_str);
            local ui_notify_start = U8toCUIStr("NotifyStartEditContent");
            pWebCtrl:CreateJsCallback(ui_notify_start);
            pWebCtrl:Navigate(ui_str);
            pWebCtrl:SetFocus();
            -- 起8秒的定时器
            base_utility.add_interval("TNewPictCheck.HandleLoadPageTimeOut", 8000);
        end
    end;

    ShowView = function(url)
        if "" ~= url then
            local language_ver = base_utility.get_current_language();
            local lang_param = "";
            if language_ver == 0 then
                lang_param = "&lang=2052";
            elseif language_ver == 1 then
                lang_param = "&lang=1028";
            elseif language_ver == 2 then
                lang_param = "&lang=1033";
            end
            url = url .. lang_param;
        end
        TNewPictCheck.url = url;
        TGPLog.LogE("NewPictChk", "ShowView:" ..TNewPictCheck.url);
        if TNewPictCheck.CheckUI(url) then
            if nil ~= TNewPictCheck.pChkView then
                TNewPictCheck.pChkView:Show(TRUE);
                TNewPictCheck.pChkView:BringToTop();
                local zParam = {};
                zParam["show"] = 1;
                comm_center.send_broadcast("Msg_WebPitShow", zParam);
            end
        end;
    end;

    HideView = function()
        if nil ~= TNewPictCheck.pChkView then
            TGPLog.LogE("NewPictChk", "HideView.");
            TNewPictCheck.pChkView:Show(FALSE);
            TNewPictCheck.UnloadWebComponent();
        end;
    end;

    IsWebPictVisible = function ()
        local isVisible = FALSE;
        if nil ~= TNewPictCheck.pChkView then
            isVisible = TNewPictCheck.pChkView:IsVisible();
        end
        return isVisible;
    end;

    -- 检查界面
    CheckUI = function(url)
        TNewPictCheck.ShowErrorLoadText(false);
        if nil ~= TNewPictCheck.pChkView then
            TNewPictCheck.UnloadWebComponent();
            TNewPictCheck.LoadWebComponent();
            return true;
        end;

        local param_tb = base_utility.split(url, "&");
        local width = 360;
        local height = 360;
        local param_count = 0;
        for k, v in pairs(param_tb) do
            local w_index = string.find(v, "sp_width");
            if nil ~= w_index then
                local w_tb = base_utility.split(v, "=");
                if nil ~= w_tb[2] then
                    width = tonumber(w_tb[2]);
                    param_count = param_count + 1;
                end
            end
            local h_index = string.find(v, "sp_height");
            if nil ~= h_index then
                local h_tb = base_utility.split(v, "=");
                if nil ~= h_tb[2] then
                    height= tonumber(h_tb[2]);
                    param_count = param_count + 1;
                end
            end
            if 2 == param_count then
                break;
            end
        end
        
        
        TNewPictCheck.pChkView = ui_utility.get_ctrl_ptr_by_path(
                        TNewPictCheck.nRootID, "pnlWebPict");

        if nil == TNewPictCheck.pChkView then
            TGPLog.LogE("NewPictChk", "Get check view fail.");
            return false;
        end;


        local nInnerPnlID = ui_utility.get_ctrl_id_by_path(TNewPictCheck.nRootID, "pnlWebPict\\pnlInnerWebPict");
        local nObWebID = ui_utility.get_ctrl_id_by_path(nInnerPnlID, "uoPict");
        ui_utility.control_resize_by_id(nInnerPnlID, width + 32, height + 76);
        ui_utility.control_resize_by_id(nObWebID, width, height);

        TNewPictCheck.UnloadWebComponent();
        TNewPictCheck.LoadWebComponent();
        return true;
    end;

    OnClose = function (uilib, controlId)
        TNewPictCheck.HideView();
    end;

    OnJsCallBack = function (event_id, json_param)
        if "" == json_param then
            return;
        end

        local param = base_utility.json_to_table(json_param);

        if nil ~= param.result and 0 == tonumber(param.result) then
            local ticket = param.ticket;
            local randstr = param.randstr;
            local data = {uin = TLoginModule.GetCurUin(), ticket = ticket, randstr = randstr};
            comm_center.call_service("LoginSvr_VerifyWebPict", data);
        end
    end;


    OnNavigateComplete = function (uilib, url)
        -- 页面加载加载完成
        TNewPictCheck.bLoadPaged = true;
    end;

    OnWebCrash = function ()
        -- qblink crash则走原来的验证码
        TNewPictCheck.ShowErrorLoadText(true);
    end;

    -- 使用老的验证码
    UserPicVerify = function ()
        local     zParam = {};

        zParam["uin"]         = TPictCheck.nCurUin;
        zParam["service_id"] = TPictCheck.nSvrID;
        zParam["code"]          = "";
        comm_center.call_service("Svr_ValidPictCode", zParam);
    end;

    HandleLoadPageTimeOut = function ()
        base_utility.remove_interval("TNewPictCheck.HandleLoadPageTimeOut");
        -- 8 秒后页面还没有加载完，则使用老的验证码
        if false == TNewPictCheck.bLoadPaged then
            TNewPictCheck.ShowErrorLoadText(true);
        end
    end;

    OnClickCloseBtn = function ()
        if nil ~= TNewPictCheck.pChkView then
            -- TNewPictCheck.pChkView:Show(FALSE);
            TGPLog.LogE("NewPictChk", "OnClickCloseBtn:");
            TNewPictCheck.HideView();
            local     zParam = {};
            zParam["show"] = 0;
            comm_center.send_broadcast("Msg_WebPitShow", zParam);
            local     zParamCancle = {};
            zParamCancle["uin"]         = TPictCheck.nCurUin;
            if TPictCheck.nSvrID == 0 then
                TPictCheck.nSvrID = ierd_tgp.CA_TGP_SERVICE_ID;
            end
            zParamCancle["service_id"] = TPictCheck.nSvrID; 
            comm_center.call_service("Svr_CancelLogin", zParamCancle);
        end
    end;

    -- 加载web组件
    LoadWebComponent = function()
        local web_component_ptr = ui_utility.get_ctrl_ptr_by_path(TNewPictCheck.nRootID, "pnlWebPict\\pnlInnerWebPict\\uoPict");
        local cotrol_id = ui_utility.get_ctrl_id_by_path(TNewPictCheck.nRootID, "pnlWebPict\\pnlInnerWebPict\\uoPict");
        if web_component_ptr ~= nil then
            local is_load = web_component_ptr:IsLoad();
            if is_load ~= TRUE then
                local result = web_component_ptr:LoadContent();
                if result ~= TRUE then
                    -- 加载web组件失败，800ms之后重试
                    local tpfui =
                        base_utility.get_component_adapter("ITpf_ui",
                            "ierd_tgp.tpf_ui.GetTPFUI();");
                    tpfui:AddInterval('TNewPictCheck.DelayLoadWebTimer', 800);
                else
                    TNewPictCheck.OpenPage(web_component_ptr:GetControlID());
                end
            end
        end
    end;

    -- 延迟加载web组件
    DelayLoadWebTimer = function()
        base_utility.log("due to load failer, again load_web_component.");
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveInterval('TNewPictCheck.DelayLoadWebTimer');
        local web_component_ptr = ui_utility.get_ctrl_ptr_by_path(user_guide_view.user_guide_ctn_id, "Ctn_WebArea");
        if web_component_ptr ~= nil then
            local is_load = web_component_ptr:IsLoad();
            if is_load ~= TRUE then
                web_component_ptr:LoadContent()
            end
            TNewPictCheck.OpenPage(web_component_ptr:GetControlID());
        end
    end;

    -- 卸载web组件
    UnloadWebComponent = function()
        local nInnerPnlID = ui_utility.get_ctrl_id_by_path(TNewPictCheck.nRootID, "pnlWebPict\\pnlInnerWebPict");
        local nObWebID = ui_utility.get_ctrl_id_by_path(nInnerPnlID, "uoPict");
        local pObWeb = ui_utility.get_ctrl_ptr_by_id(nObWebID);
        if nil == pObWeb then
            return;
        end

        pObWeb:UnLoadContent();
    end;

    on_hyperlink_click = function(uilib, dwControlId, hyberlink_rc, hyberlink_id, hyberlink_name, left, top, keystate, ctrl,is_shift,is_alt, hyberlink)
        local hyberlink_id_text = string.format("%d", hyberlink_id);
        TGPLog.LogI("new_pic_check", "on_hyperlink_click:" ..hyberlink_id_text);
        --  hyberlink_id是在language.xml 处ID_TPF_LOGIN_LOAD_NEW_PIC_URL_FAILED设定的
        if hyberlink_id == 1 then
            TNewPictCheck.ShowErrorLoadText(false);
            local web_component_ptr =
            ui_utility.get_ctrl_ptr_by_path(TNewPictCheck.nRootID, "pnlWebPict\\pnlInnerWebPict\\uoPict");
            local pWebCtrl = ui_utility.get_ctrl_ptr_by_path(web_component_ptr:GetControlID(), "QBWebBrowser");
            if pWebCtrl ~= nil then
                pWebCtrl:Refresh();
                base_utility.add_interval("TNewPictCheck.HandleLoadPageTimeOut", 8000);
            else
                TNewPictCheck.ShowView(TNewPictCheck.url);
            end;
        end;
    end;

    cal_text_width = function (str)
        local width = string.len(str);
        local calc_ctrl_id = ui_utility.get_ctrl_id_by_path(TLoginView.pDlgLogin:GetControlID(), "pnlWebPict\\pnlInnerWebPict\\DummyCtrlForWidthCalc");
        if Tenio.INVALID_CONTROLID == calc_ctrl_id then
            return width;
        end
        ui_utility.setproperty_by_id(calc_ctrl_id, "Caption", str);
        local cal_width = ui_utility.get_control_size_by_id(calc_ctrl_id);
        if 0 ~= cal_witdh  then
            width = cal_width;
        end
        return width;
    end;

    ShowErrorLoadText = function(is_show)
        if TLoginView.pDlgLogin == nil then
            TGPLog.LogE("NewPictChk", "ShowErrorLoadText pDlgLogin is nil");
            return;
        end
        local erro_load_rich_text_ptr = ui_utility.get_ctrl_ptr_by_path(
                         TLoginView.pDlgLogin:GetControlID(), "pnlWebPict\\pnlInnerWebPict\\ErrorLoadRichText");
        local nInnerPnlID = ui_utility.get_ctrl_id_by_path(TNewPictCheck.nRootID, "pnlWebPict\\pnlInnerWebPict");
        local nObWebID = ui_utility.get_ctrl_id_by_path(nInnerPnlID, "uoPict");
        if is_show == true then
            ui_utility.setproperty_by_id(nObWebID, "Visible", "false");
        else
            ui_utility.setproperty_by_id(nObWebID, "Visible", "true");
        end
        if erro_load_rich_text_ptr~= nil and erro_load_rich_text_ptr ~= 0 then
            local text = LoadStr("ID_TPF_LOGIN_LOAD_NEW_PIC_URL_FAILED");
            local uText = U8toCUIStr(text);
            erro_load_rich_text_ptr:Clear();
            erro_load_rich_text_ptr:PushBackHtmlString(uText);
            if is_show == false then
                erro_load_rich_text_ptr:Show(FALSE);
            else
                erro_load_rich_text_ptr:Show(TRUE);
            end;
        end;
    end;
    -- 界面控件
    nRootID  = 0;
    pChkView = nil;
    bLoadPaged = false;
    url = "";
};